﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/contactGrp">
    <contactGrp name="אנשי קשר">
      <xsl:apply-templates select="contactGrp"/>
    </contactGrp>
  </xsl:template>

  <xsl:template match="contactGrp/contactGrp">
    <contactGrp>
      <xsl:attribute name="name">
        <xsl:value-of select="@name"/>
      </xsl:attribute>
      
      <xsl:for-each select="contactGrp">
      <xsl:sort select="@name"/>
      </xsl:for-each>

      <xsl:for-each select="customer">
        <xsl:sort select="@name"/>
        <xsl:copy-of select="."/>
      </xsl:for-each>

    </contactGrp>
  </xsl:template>

  <xsl:template match="contactGrp/contactGrp/customer">
    <customer>
      <xsl:attribute name="name">
        <xsl:value-of select="@type"/>
      </xsl:attribute>

      <xsl:for-each select="email">
        <xsl:copy-of select="."/>
      </xsl:for-each>

    </customer>
  </xsl:template>

</xsl:stylesheet>